const test_method = require("./test-method");
const _ = require("lodash");

const desktopConfig = JSON.parse(JSON.stringify(require('../config/lr-desktop-config')));
const throttlings = require('../config/setting-throttling-config');

let singleReportTitle = "mBlock-forum";
let summaryReportTile = "mBlock Forum Page Performance Test Report";

const url = "https://mblock.makeblock.com";

/**
 * 测试：在桌面端模拟不同网络测试
 */
async function test_diff_throttling_on_desktop() {
    let indicatorList = [];
    for (index in throttlings) {
        desktopConfig.settings.throttling = throttlings[index];
        let test_result = await test_method.lr_run(url, desktopConfig, singleReportTitle);
        indicatorList.push({ 
            throttling: throttlings[index], 
            indicator: test_result.indicator, 
            reportName: test_result.singleReportName });
    }
    return indicatorList;
}

/**
 * 在默认环境下测试
 */
async function test_provided_on_desktop() {
    let config = require('../config/lr-provided-config');
    let indicatorList = [];
    let test_result = await test_method.lr_run(url, config, singleReportTitle);
    indicatorList.push({ 
            throttling: {rttMs:"default",  throughputKbps:"default", cpuSlowdownMultiplier: "default"},
            indicator: test_result.indicator, 
            reportName: test_result.singleReportName});
    return indicatorList;
}

/**
 * 以登录态测试
 */
async function test_projectlist_under_user_login(){
    let config = JSON.parse(JSON.stringify(require('../config/lr-provided-config')));
    
    config.settings.extraHeaders = {utoken: "00qr41jm"};
    let indicatorList = [];
    let test_result = await test_method.lr_run(url, config, singleReportTitle);
    indicatorList.push({ 
            throttling: {rttMs:"default",  throughputKbps:"default", cpuSlowdownMultiplier: "default"},
            indicator: test_result.indicator, 
            reportName: test_result.singleReportName});
    return indicatorList;
}


async function run_all() {
    let result_throttling = await test_diff_throttling_on_desktop();
    let result_provided = await test_provided_on_desktop();
    let result = _.unionWith(result_throttling, result_provided)
    await test_method.outputSummaryReport(result, summaryReportTile)
    return result;
}

module.exports = run_all

run_all().then(result => {
    console.log(result);
}).catch(error => {
    console.log('测试出错', error);
})